﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Landscape.Business.FNH;
using Landscape.Business.FNH.Activities;
using Landscape.Business;

namespace Landscape.WebApp
{
	public class BasePage : System.Web.UI.Page
	{
		private bool canSearch = false;
		private bool canEdit = false;
		private bool canDelete = false;
		private bool canApprove = false;
                
		public string PageTitle
		{ get; set; }

		public bool CanSearch
		{ get { return this.canSearch; } set { this.canSearch = value; } }

		public bool CanEdit
		{
			get { return this.canEdit; }
			set
			{
				this.canEdit = value;
				if(value)
					this.canSearch = value;
			}
		}

		public bool CanDelete
		{
			get { return this.canDelete; }
			set
			{
				this.canDelete = value;
				if (value)
				{
					this.canEdit = value;
					this.canSearch = value;
				}
			}
		}

		public bool CanApprove
		{ get { return this.canApprove; } set { this.canApprove = value; } }

		public Guid PageID
		{
			get
			{
				string curPage = this.Request.Url.Segments[this.Request.Url.Segments.Count() - 1];
				return Global.AppInternalModules.Where(exp => exp.Url != null && exp.Url.ToLower().Contains(curPage.ToLower())).Single().ID;
			}
		}

		public void Page_Load(object sender, EventArgs e)
		{
			if (Cache["NAV_ITEMS"] != null)
			{
				List<UIBizObjects.NavItem> navItems = (List<UIBizObjects.NavItem>)Cache["NAV_ITEMS"];

			}

            if (!IsPostBack)
            {
                using (var nhSession = NHSessionManager.GetManager().GetSession())
                {
                    AdminActivities aa = new AdminActivities(nhSession);
                    if (!CurrentUser.IsSysAdmin)
                    {
                        var privs = aa.GetUserPrivilegesFor(PageID, CurrentUser.UserID);
                        foreach (var p in privs)
                        {
                            switch (p.PrivilegeCode)
                            {
                                case Business.ModulePrivilegeCodes.APPROVE:
                                    {
                                        this.CanApprove = true;
                                        break;
                                    }
                                case Business.ModulePrivilegeCodes.DELETE:
                                    {
                                        this.CanDelete = true;
                                        break;
                                    }
                                case Business.ModulePrivilegeCodes.EDIT:
                                    {
                                        this.CanEdit = true;
                                        break;
                                    }
                                case Business.ModulePrivilegeCodes.SEARCH:
                                    {
                                        this.CanSearch = true;
                                        break;
                                    }
                            }
                        }
                    }
                    else
                    {
                        this.CanApprove = true;
                        this.CanDelete = true;
                    }
                }
            }
		}

		public void Page_PreRender(object sender, EventArgs e)
		{
			this.Title = this.PageTitle;
		}

		public Landscape.WebApp.UIBizObjects.ActiveUser CurrentUser
		{
			get
			{
				return (Landscape.WebApp.UIBizObjects.ActiveUser)Session["ACTIVE_USER"];
			}
		}
	}
}